﻿using System;
using System.DirectoryServices;

namespace JQuery.WebKit.Common.ActiveDirectory
{
    [Serializable]
    public class UserInfo : ADObject, IComparable
    {
        private string _locationCode;
        public string LocationCode
        {
            get { return _locationCode; }
            set { _locationCode = value; }
        }

        private string _firstName;
        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }

        private string _lastName;
        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }


        private string _emailAddress;
        public string EmailAddress
        {
            get { return _emailAddress; }
            set { _emailAddress = value; }
        }

        private bool _accountDisabled;
        public bool AccountDisabled
        {
            get { return _accountDisabled; }
            set { _accountDisabled = value; }
        }

        private string _currentRole;
        public string CurrentRole
        {
            get { return _currentRole; }
            set { _currentRole = value; }
        }

        public override ADObjectType ObjectType
        {
            get { return ADObjectType.User; }
        }

        public UserInfo()
            : this(string.Empty, ADContextType.Unknown, null)
        {

        }

        public UserInfo(string accountName, ADContextType contextType, string distinguishedName)
            : base(accountName, contextType, distinguishedName)
        {
        }

        public static UserInfo GetUserInfo(DirectoryEntry userEntry, ADContextType userContextType)
        {
            if (userEntry == null)
            {
                return null;
            }

            PropertyValueCollection accountName = userEntry.Properties["samaccountname"];
            PropertyValueCollection dn = userEntry.Properties["distinguishedName"];

            if (accountName == null || dn == null ||
                accountName.Value == null || dn.Value == null)
            {
                return null;
            }

            UserInfo info = new UserInfo(accountName.Value.ToString(), userContextType, dn.Value.ToString());
            foreach (PropertyValueCollection prop in userEntry.Properties)
            {
                if (prop.Value == null)
                {
                    continue;
                }
                Console.WriteLine(string.Format("{0} - {1}", prop.PropertyName, prop.Value.ToString()));
                switch (prop.PropertyName.ToLower())
                {
                    case "cn":
                    case "displayname":
                        info.DisplayName = prop.Value.ToString();
                        break;
                    case "distinguishedname": 
                        break;
                    case "mail":
                        info.EmailAddress = prop.Value.ToString();
                        break;
                    case "givenname":
                        info.FirstName = prop.Value.ToString();
                        break;
                    case "sn":
                        info.LastName = prop.Value.ToString();
                        break;
                    default:
                        break;
                }
            }

            //info.AccountDisabled = userPrincipal.Enabled.HasValue ? !userPrincipal.Enabled.Value : false;
            //info.EmailAddress = userPrincipal.EmailAddress;
            //info.FirstName = userPrincipal.Name;
            //info.LastName = userPrincipal.Surname;
            //info.LocationCode = userPrincipal.Context.Name;
            //info.DisplayName = string.IsNullOrEmpty(userPrincipal.DisplayName) ? userPrincipal.Name : userPrincipal.DisplayName;

            return info;
        }

        #region IComparable Members

        public int CompareTo(object obj)
        {
            if (obj is UserInfo)
            {
                UserInfo userInfo = (UserInfo)obj;
                int cmp = this.DistinguishedName.CompareTo(userInfo.FirstName);
                if (cmp == 0)
                {
                    cmp = this.EmailAddress.CompareTo(userInfo.LastName);
                }
                return cmp;
            }
            throw new ArgumentException(string.Format("Cannot compare a UserInfo to an object of type {0}", obj.GetType().ToString()));
        }

        #endregion
    }
}
